#! /bin/bash

### BEGIN INIT INFO
# Provides:             opcda
# Required-Start:       $local_fs $network
# Required-Stop:        $local_fs $network
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    DC3 Opc Da Driver
# Description:          DC3 Opc Da Driver Service Daemon
### END INIT INFO

set -e

JAR_FILE="dc3-driver-opc-da.jar"
BASE_DIR="/home/pi/iot-dc3/dc3-driver/dc3-driver-opc-da/target"
JAVA_OPS="-server -Xms128m -Xmx512m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m"

start()
{
export JAVA_HOME=/usr/local/java/jdk1.8.0_251
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib

cd $BASE_DIR
nohup java $JAVA_OPS -Djava.security.egd=file:/dev/./urandom -jar $JAR_FILE >/dev/null &
}

stop()
{
pid=`ps -ef|grep $JAR_FILE |grep -v grep |awk '{print $2}'`
kill -9 $pid
}

case "$1" in

start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

*)
esac
exit 0